﻿using OF.Component.BLL;
using OF.Component.Model;
using OF.Utility;
using OpSystem.common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace OpSystem.manage.city
{
    public partial class AddCity : BasePage
    {

        protected string strMessage = "";
        public int Id
        {
            get
            {
                return EConvert.ToInt(Request.QueryString["id"], 0);
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            CheckPageCode("10202");
            if (!IsPostBack)
            {
                SetBind();
                if (Id > 0)
                {
                    EditCityBind();
                }
            }

        }


        protected void btnSave_Click(object sender, EventArgs e)
        {
            string MessageInfo = "信息修改成功！";
            BaseLocationInfo info = null;
            int provinceId = int.Parse(this.ddlProvince.SelectedValue);
            if (Id > 0)
            {
                info = BaseLocationOperate.GetByID(Id);
                BaseLocationInfo oldinfo = ObjectCopy<BaseLocationInfo>.DeepCopy(info);
                info.LocationName = this.txtLocationName.Text;
                info.LocationShortName = this.txtLocationShortName.Text;
                info.LocationSpellName = this.txtLocationSpellName.Text;
                info.LocationParentID = provinceId;
                info.LocationRemark = this.txtRemark.Text;
                BaseLocationOperate.Update(info);
                // BaseOperationLog.SetLog(oldinfo, info, OpUserID, OpUserInfo.OpUserName, 0, Guid.NewGuid().ToString(), (short)enumCommon.LogType.开通城市管理);
            }
            else
            {
                info = new BaseLocationInfo();
                info.LocationID = BaseLocationOperate.GetGeographyTypeMaxId(provinceId,1);
                info.LocationName = this.txtLocationName.Text;
                info.LocationParentID = provinceId;
                info.LocationSpellName = this.txtLocationSpellName.Text;
                info.LocationShortName = this.txtLocationShortName.Text;
                info.LocationRemark = this.txtRemark.Text;
                info.LocationStatus = 1;
                info.LocationGeographyTypeID = 1;
                var pId = BaseLocationOperate.Insert(info);
                MessageInfo = "信息创建成功！";
                //BaseOperationLog.SetLog(null, info, OpUserID, OpUserInfo.OpUserName, 0, Guid.NewGuid().ToString(), (short)enumCommon.LogType.开通城市管理);
            }
            strMessage = " BMCW.msg(\"" + MessageInfo + "\",\"" + GetPageUrl("CityList.aspx") + "\",true);";
        }

        private void EditCityBind()
        {
            BaseLocationInfo info = BaseLocationOperate.GetByID(Id);
            if (info != null)
            {
                this.txtLocationName.Text = info.LocationName;
                this.txtLocationShortName.Text = info.LocationShortName;
                this.txtLocationSpellName.Text = info.LocationSpellName;
                BaseLocationOperate.BindLocation(ref this.ddlProvince, 0);
                this.ddlProvince.SelectedValue = info.LocationParentID.ToString();

            }
        }

        private void SetBind()
        {
            BaseLocationOperate.BindLocation(ref this.ddlProvince,0);
        }

      
    }
}
